package cn.guozeyiblog.blog;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import me.maxwin.view.XListView;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import cn.guozeyiblog.blog.utils.Content;
import cn.guozeyiblog.blog.utils.NetTools;
import cn.guozeyiblog.blog.utils.Statictool;

class MyPagerAdapter extends PagerAdapter {
	private final static int PAGERVIEW_COUNT = 4;
	private MainXListView mainXListView;
	private HotXListView hotXListView;
	private WitterListView witterListView;
	private static Context context;
	private ArrayList<View> views;
	private static final int LOAD = 1;
	private static final int UPDATE = 2;
	private static final int HOTLOAD = 3;
	private static final int HOTUPDATE = 4;
	private static final int CATUPDATE = 5;
	private static final int WLOAD = 6;
	private static final int WUPDATE = 7;
	private ArrayAdapter<String> adapterMain;
	private ArrayAdapter<String> adapterHot;
	private ArrayAdapter<String> adapterCat;
	private ArrayAdapter<String> adaptertwitter;
	private XListView  view1;
	private ListView lvView2;
	private XListView  view3;
	private XListView  view4;
	private ArrayList<String> mainObject = new ArrayList<>();
	private ArrayList<String> hotObject = new ArrayList<>();
	private ArrayList<String> catObjects = new ArrayList<>();
	private ArrayList<String> twitterObjects = new ArrayList<>();
	private static ArrayList<String> link = new ArrayList<>();
	private static ArrayList<String> hotlink = new ArrayList<>();
	private static ArrayList<String> catlink = new ArrayList<>();
	private SharedPreferences preferences; 
	private int addcount=0;
	private Handler handler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case LOAD:
				ArrayList<Content> load = new ArrayList<Content>();
				addcount = 0;
				load = (ArrayList<Content>) msg.obj;
				for (Content c:load) {
					if (!mainObject.contains(c.gettitle())) {
                       mainObject.add(c.gettitle());
                       link.add(c.getLink());
                       addcount++;
					}
				}
				if(addcount==0&&load.size()<10){
					Toast.makeText(context, "没有更多数据", 0).show();
				}
				if (addcount==0&&load.size()==10&&mainXListView!=null) {
					mainXListView.onLoadMore();
					return ;
				}
				adapterMain.notifyDataSetChanged();
				break;
			case UPDATE:
				ArrayList<Content> updata = new ArrayList<Content>();
				updata = (ArrayList<Content>) msg.obj;
				addcount=0; //清除记录
				for (Content c:updata) {
					if (!mainObject.contains(c.gettitle())) {
                       mainObject.add(0,c.gettitle());
                       link.add(0,c.getLink());
                       addcount++;
					}
				}
				if(addcount==0){
					Toast.makeText(context, "没有新数据", 0).show();
				}
				adapterMain.notifyDataSetChanged();
				//把前10条数据保存在本地
				SharedPreferences.Editor editor = preferences.edit();
				int lenght = mainObject.size()>10?10:mainObject.size();
				for (int i = 0; i < lenght; i++) {
					editor.putString("title"+i,mainObject.get(i));
					editor.putString("link"+i,link.get(i));
				}
				editor.commit();
				break;
				
				case HOTUPDATE:
					ArrayList<Content> hotupdata = new ArrayList<Content>();
					hotupdata = (ArrayList<Content>) msg.obj;
					addcount=0; //清除记录
					for (Content c:hotupdata) {
						if (!hotObject.contains(c.gettitle())) {
							hotObject.add(0,c.gettitle());
	                       hotlink.add(0,c.getLink());
	                       addcount++;
						}
					}
					if(addcount==0){
						Toast.makeText(context, "没有新数据", 0).show();
					}
					adapterHot.notifyDataSetChanged();
					//把前10条数据保存在本地
					SharedPreferences.Editor hoteditor = preferences.edit();
					int hotlenght = hotObject.size()>10?10:hotObject.size();
					for (int i = 0; i < hotlenght; i++) {
						hoteditor.putString("hottitle"+i,hotObject.get(i));
						hoteditor.putString("hotlink"+i,hotlink.get(i));
					}
					hoteditor.commit();
					break;
				case HOTLOAD:
					ArrayList<Content> hotload = new ArrayList<Content>();
					addcount = 0;
					hotload = (ArrayList<Content>) msg.obj;
					for (Content c:hotload) {
						if (!hotObject.contains(c.gettitle())) {
	                       hotObject.add(c.gettitle());
	                       hotlink.add(c.getLink());
	                       addcount++;
						}
					}
					if(addcount==0&&hotload.size()<10){
						Toast.makeText(context, "没有更多数据", 0).show();
					}
					if (addcount==0&&hotload.size()==10&&hotXListView!=null) {
						hotXListView.onLoadMore();
						return ;
					}
					adapterHot.notifyDataSetChanged();
					break;
					
				case CATUPDATE:
					ArrayList<Content> cat = (ArrayList<Content>) msg.obj;
					for (Content c: cat) {
						if (!catObjects.contains(c.gettitle())) {
							catObjects.add(c.gettitle());
							catlink.add(c.getLink());
						}
					}
					SharedPreferences.Editor cateditor = preferences.edit();
					for (int i = 0; i < catObjects.size(); i++) {
						cateditor.putString("cattitle"+i,catObjects.get(i));
						cateditor.putString("catlink"+i,catlink.get(i));
					}
					cateditor.commit();
					
					adapterCat.notifyDataSetChanged();
					break;
				case WLOAD:
					ArrayList<Content> wload = new ArrayList<Content>();
					addcount = 0;
					wload = (ArrayList<Content>) msg.obj;
					for (Content c:wload) {
						if (!twitterObjects.contains(c.gettitle())) {
							twitterObjects.add(c.gettitle());
	                       //hotlink.add(c.getLink());
	                       addcount++;
						}
					}
					if(addcount==0&&wload.size()<10){
						Toast.makeText(context, "没有更多数据", 0).show();
					}
					if (addcount==0&&wload.size()==10&&hotXListView!=null) {
						witterListView.onLoadMore();
						return ;
					}
					adaptertwitter.notifyDataSetChanged();
					break;
				case WUPDATE:
					ArrayList<Content> wupdate = new ArrayList<Content>();
					addcount = 0;
					wupdate = (ArrayList<Content>) msg.obj;
					for (Content c:wupdate) {
						if (!twitterObjects.contains(c.gettitle())) {
							twitterObjects.add(0,c.gettitle());
	                       //hotlink.add(c.getLink());
	                       addcount++;
						}
					}
					if(addcount==0&&wupdate.size()<10){
						Toast.makeText(context, "没有更多数据", 0).show();
					}
					if (addcount==0&&wupdate.size()==10&&hotXListView!=null) {
						witterListView.onLoadMore();
						return ;
					}
					adaptertwitter.notifyDataSetChanged();
					//把前10条数据保存在本地
					SharedPreferences.Editor weditor = preferences.edit();
					int wlenght = twitterObjects.size()>10?10:twitterObjects.size();
					for (int i = 0; i < wlenght; i++) {
						weditor.putString("twittertitle"+i,twitterObjects.get(i));
						//weditor.putString("hotlink"+i,hotlink.get(i));
					}
					weditor.commit();
					break;

			default:
				break;
			}
			view1.stopLoadMore();
			view1.stopRefresh();
			view3.stopLoadMore();
			view3.stopRefresh();
			view4.stopRefresh();
			view4.stopLoadMore();
			SimpleDateFormat format = new SimpleDateFormat("yy年MM月dd日  hh:mm:ss");
			if (msg.what==UPDATE||msg.what==LOAD) {
				view1.setRefreshTime(format.format(new Date()));
			}
			if (msg.what==HOTUPDATE||msg.what==HOTLOAD) {
				view3.setRefreshTime(format.format(new Date()));
			}
			if (msg.what==WLOAD||msg.what==WUPDATE) {
				view4.setRefreshTime(format.format(new Date()));
			}
			
		};
	};
public MyPagerAdapter(final Context context) {
	// TODO Auto-generated constructor stub
	this.context = context;
	//读取上次得到的数据
	preferences = context.getSharedPreferences("data", 0);
	for (int i = 0;i<10;i++) {
		if (!preferences.getString("title"+i, "本地没有数据").equals("本地没有数据")) {
			mainObject.add(preferences.getString("title"+i, "本地没有数据"));
			link.add(preferences.getString("link"+i, "http://www.guozeyiblog.cn"));
		}
	}
	
	for (int i = 0; i < 10; i++) {
		if (!preferences.getString("hottitle"+i, "本地没有数据").equals("本地没有数据")) {
		    hotObject.add(preferences.getString("hottitle"+i, "本地没有数据"));
			hotlink.add(preferences.getString("hotlink"+i, "http://www.guozeyiblog.cn"));
		}
	}
	
	for (int i = 0; i < 10; i++) {
		if (!preferences.getString("cattitle"+i, "本地没有数据").equals("本地没有数据")) {
		    catObjects.add(preferences.getString("cattitle"+i, "本地没有数据"));
			catlink.add(preferences.getString("catlink"+i, "http://www.guozeyiblog.cn"));
		}
	}
	
	for (int i = 0; i <10; i++) {
		if (!preferences.getString("twittertitle"+i, "本地没有数据").equals("本地没有数据")) {
		    twitterObjects.add(preferences.getString("twittertitle"+i, "本地没有数据"));
			//hotlink.add(preferences.getString("hotlink"+i, "http://www.guozeyiblog.cn"));
		}
	}
	
	/**
	 * 1.主页 XlistView 
	 * 2.分类 
	 * 3.热门 XlistView
	 * 4.微语 
	 */
	
	//主页
	views = new ArrayList<>(); //用于ViewPager
	view1 = new XListView(context);
	adapterMain = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, mainObject);
	view1.setAdapter(adapterMain);
	mainXListView = new MainXListView(handler, context);
	view1.setXListViewListener(mainXListView);
	view1.setOnItemClickListener(mainXListView);
	view1.setPullLoadEnable(true);
	
	//分类
	lvView2 = new ListView(context);
	adapterCat = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, catObjects);
	
	new Thread(){public void run() {
	   //php的接口没有实现	
		ArrayList<Content> cat = NetTools.DoHttp(Statictool.CATLIST);
        Message msg = new Message();
        msg.what = CATUPDATE;
        msg.obj = cat;
        handler.sendMessage(msg);
	};}.start();;
	lvView2.setAdapter(adapterCat);
	lvView2.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Toast.makeText(context, catlink.get(position), 0).show();
			//此处实现Activity的跳转
			//在新的XListView中实现加载 刷新的功能
			
		}
	});
	
	//热门
	view3 = new XListView(context);
	adapterHot = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, hotObject);
	view3.setAdapter(adapterHot);
	hotXListView = new HotXListView(handler,context);
	view3.setOnItemClickListener(hotXListView);
	view3.setXListViewListener(hotXListView);
	view3.setPullLoadEnable(true);
	
	//微语
	view4 = new XListView(context);
	adaptertwitter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, twitterObjects);
	view4.setAdapter(adaptertwitter);
	witterListView = new WitterListView(handler,context);
	view4.setOnItemClickListener(witterListView);
	view4.setPullLoadEnable(true);
	view4.setXListViewListener(witterListView);
	
	views.add(view1);
	views.add(lvView2);
	views.add(view3);
	views.add(view4);
}
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return PAGERVIEW_COUNT;
	}

	@Override
	public boolean isViewFromObject(View arg0, Object arg1) {
		// TODO Auto-generated method stub
		return arg0==arg1;
	}
	
	@Override
	public Object instantiateItem(View container, int position) {
		// TODO Auto-generated method stub
			 ((ViewPager)container).addView(views.get(position));
			 return views.get(position); 

	}
	
	@Override
	public void destroyItem(ViewGroup container, int position, Object object) {
		// TODO Auto-generated method stub
		container.removeView(views.get(position));
	}

	public static ArrayList<String> getLink() {
		return link;
	}
	
	public static ArrayList<String> getHotlink() {
		return hotlink;
	}


	
}